package org.inspursc.s0517.health.evaluation.utils;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

/**
 * @author B_Dream
 * @version ：2018/7/11 9:25
 */
public class ReadTextUtil {

    public static String readTextToStr(HttpServletRequest request) {

        try {
            MultipartFile file = ((MultipartHttpServletRequest) request)
                    .getFiles("file").get(0);
            String encoding = "UTF-8";
            if (file != null) {
                //截取文件后缀名
                String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                if (".json".equals(suffix)) {
                    try {
                        return new String(file.getBytes(), encoding);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                        return null;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }


}
